#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>


#define LAYOUT "queue"
#define CREATE_MODE_RW (S_IWRITE | S_IREAD)


namespace
{


enum class queue_op {
	UNKNOWN_QUEUE_OP,
	QUEUE_PUSH,
	QUEUE_POP,
	QUEUE_SHOW,

	MAX_QUEUE_OP,
};


queue_op parse_queue_op(const char *str) {
	if (strcmp(str, "show") == 0)
		return queue_op::QUEUE_SHOW;
	else if (strcmp(str, "push") == 0)
		return queue_op::QUEUE_PUSH;
	else if (strcmp(str, "pop") == 0)
		return queue_op::QUEUE_POP;
	else
		return queue_op::UNKNOWN_QUEUE_OP;
}
}


namespace examples
{


class pmem_queue {

	struct pmem_entry {   //相当于一个节点
		pmem::obj::persistent_ptr<pmem_entry> next;
		pmem::obj::p<uint64_t> value;
	};

public:

	void push(pmem::obj::pool_base &pop, uint64_t value)
	{
		pmem::obj::transaction::run(pop, [&] {
			auto n = pmem::obj::make_persistent<pmem_entry>();

			n->value = value;
			n->next = nullptr;

			if (head == nullptr && tail == nullptr) {
				head = tail = n;
			} else {
				tail->next = n;
				tail = n;
			}
		});
	}

	uint64_t pop(pmem::obj::pool_base &pop)
	{
		uint64_t ret = 0;
		pmem::obj::transaction::run(pop, [&] {
			if (head == nullptr)
				pmem::obj::transaction::abort(EINVAL);

			ret = head->value;
			auto n = head->next;

			pmem::obj::delete_persistent<pmem_entry>(head);
			head = n;

			if (head == nullptr)
				tail = nullptr;
		});

		return ret;
	}

	void show(void) const
	{
		for (auto n = head; n != nullptr; n = n->next)
			std::cout << n->value << std::endl;
	}

private:
	pmem::obj::persistent_ptr<pmem_entry> head;
	pmem::obj::persistent_ptr<pmem_entry> tail;
};

} 
static inline int file_exists(char const *file) {
	return access(file, F_OK);
}
int main(int argc, char *argv[])
{
	if (argc < 3) {
		std::cerr << "usage: " << argv[0] << " file-name [push [value]|pop|show]" << std::endl;
		return 1;
	}

	const char *path = argv[1];

	queue_op op = parse_queue_op(argv[2]);

	pmem::obj::pool<examples::pmem_queue> pop;
	pmem::obj::persistent_ptr<examples::pmem_queue> q;

	try {
		if (file_exists(path) != 0) {
			pop = pmem::obj::pool<examples::pmem_queue>::create(path, LAYOUT, PMEMOBJ_MIN_POOL, CREATE_MODE_RW);
		} else {
			pop = pmem::obj::pool<examples::pmem_queue>::open(path, LAYOUT);
		}
		q = pop.root();
	} catch (const pmem::pool_error &e) {
		std::cerr << "Exception: " << e.what() << std::endl;
		return 1;
	} catch (const pmem::transaction_error &e) {
		std::cerr << "Exception: " << e.what() << std::endl;
		return 1;
	}

	switch (op) {
		case queue_op::QUEUE_PUSH:
			try {
				q->push(pop, std::stoull(argv[3]));
			} catch (const std::runtime_error &e) {
				std::cerr << "Exception: " << e.what() << std::endl;
				return 1;
			}
			break;
		case queue_op::QUEUE_POP:
			try {
				std::cout << q->pop(pop) << std::endl;
			} catch (const std::runtime_error &e) {
				std::cerr << "Exception: " << e.what()
					  << std::endl;
				return 1;
			} catch (const std::logic_error &e) {
				std::cerr << "Exception: " << e.what()
					  << std::endl;
				return 1;
			}
			break;
		case queue_op::QUEUE_SHOW:
			q->show();
			break;
		default:
			std::cerr << "Invalid queue operation" << std::endl;
			return 1;
	}

	try {
		pop.close();
	} catch (const std::logic_error &e) {
		std::cerr << "Exception: " << e.what() << std::endl;
		return 1;
	}
	return 0;
}